from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
import threading
import queue
import configparser
import os
import time
import addTitle
# 返回浏览器驱动
def getDriver():
    start = time.time()
    chrome_options = Options()    
    #chrome_options.add_argument('--show-component-extension-options')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--args --disable-web-security --user-data-dir')
    driver = webdriver.Chrome(executable_path='chromedriver.exe',options=chrome_options)
    #driver = webdriver.PhantomJS(executable_path='phantomjs.exe')
    end = time.time()
    print('----------------创建浏览器驱动花费时间:' + str(end-start) + '----------------')    
    return driver
# 重写driver中的get_screenshot_as_file方法
def get_screenshot_as_file(driver,path,url,config):
    driver.get(url)
    png = driver.get_screenshot_as_png()
    addTitle.addTitle(png,path,url,config)    
    return True
# 访问对应url保存渲染图片
def saveImgByUrl(driver,url,fileName):
    driver.get(url)
    driver.get_screenshot_as_file(fileName)
class ScreenShot():
    def __init__(self,url,index):
        self.url = url
        self.index = index
#生产者 从文本获取url并放入队列中
class Producer(threading.Thread): 
    def __init__(self,file,list):
        threading.Thread.__init__(self)
        self.file = file
        self.list = list
    def run(self):
        for obj in self.list:
            obj.start()
        i = 0 
        print('----------------读取urlFileName:' + self.file + '----------------')
        start = time.time()
        index = 1
        try:
            #f= open("file1.txt",encoding='utf-8')
            #file_data=f.read() #完全读取
            #print(file_data)
            for line in open(self.file):
                if line.strip() == "" :
                    continue
                self.list[i].queue.put(ScreenShot(line.strip(),index))
                index += 1
                i += 1
                i = i if i < len(self.list) else 0
        finally:         
            for obj in self.list:
                obj.cleanFlag()
        end = time.time()
        print('----------------读取文件花费时间:' + str(end-start) + ',共有' + str(index) + '个url----------------')
        print('----------------读取urlFileName结束----------------')
            
        
#消费者 从队列中获取url并截图
class Consumer(threading.Thread):
    def __init__(self,name,driver,queue,path,config):
        threading.Thread.__init__(self)
        self.name = name
        self.driver = driver
        self.queue = queue
        self.path = path
        self.config = config
    def run(self):
        """
        执行访问和截图
        """ 
        print('---------------name:' + str(self.name) + ' 正在截图 请勿关闭窗口----------------')
        try:
            self.flag = False
            while True:
                screenShot = self.queue.get()
                fileName = self.path + '\\' + str(screenShot.index) + '.jpg'
                if self.config.isAddTitle == 'true':
                    get_screenshot_as_file(self.driver,fileName, screenShot.url, self.config)
                else:
                    saveImgByUrl(self.driver,screenShot.url,self.path + '\\' + str(screenShot.index) + '.jpg')
                #print('---------------flag:' + str(self.flag) + ',qsize:' + str(self.queue.qsize()) + ',name:' + str(self.name) + '----------------')   
                if self.flag and self.queue.empty():
                    print('---------------name:' + str(self.name) + ' 结束了----------------')   
                    break                    
        finally:
            self.driver.quit()
    def cleanFlag(self):
        self.flag = True
# 读取配置文件
class Config():
    def __init__(self,conf):        
        print('----------------读取配置----------------')
        self.isAddTitle = conf.get('CONFIG','is_add_title')                  
        print('----------------isAddTitle = ' + self.isAddTitle) 
        self.imgFolder = conf.get('CONFIG','imgFolder')                  
        print('----------------imgFolder = ' + self.imgFolder)
        self.urlFileName = conf.get('CONFIG','urlFileName')         
        print('----------------urlFileName = ' + self.urlFileName)
        self.driverSize = conf.getint('CONFIG','driverSize')               
        print('----------------driverSize = ' + str(self.driverSize))
        self.wordSize = conf.getint('IMAGE','word_size')               
        print('----------------wordSize = ' + str(self.wordSize))
        self.wordCss = conf.get('IMAGE','word_css')               
        print('----------------wordCss = ' + self.wordCss)
        self.wordRgb = conf.get('IMAGE','word_rgb')               
        print('----------------wordRgb = ' + self.wordRgb)
        self.wordShowLen = conf.getint('IMAGE','word_show_len')               
        print('----------------wordShowLen = ' + str(self.wordShowLen))
        self.wordShowSplit = conf.getint('IMAGE','word_show_split')               
        print('----------------wordShowSplit = ' + str(self.wordShowSplit))
        self.wordX = conf.getint('IMAGE','word_x')               
        print('----------------wordX = ' + str(self.wordX))
        self.wordY = conf.getint('IMAGE','word_y')               
        print('----------------wordY = ' + str(self.wordY))
        self.titleImg = conf.get('IMAGE','title_img')               
        print('----------------titleImg = ' + self.titleImg)
        self.titleImgHeight = conf.getint('IMAGE','title_img_height')               
        print('----------------titleImgHeight = ' + str(self.titleImgHeight))
        self.imgWidth = conf.getint('IMAGE','img_width')               
        print('----------------imgWidth = ' + str(self.imgWidth))
        self.imgHeight = conf.getint('IMAGE','img_height')               
        print('----------------imgHeight = ' + str(self.imgHeight))
        print('----------------读取配置结束----------------')

flag = False
def main():
    conf = configparser.ConfigParser()
    conf.read('config.ini',encoding = 'utf-8')
    config = Config(conf)
    imgFolder = config.imgFolder
    path = ''
    i = 1
    while True:
        suffix = '_' + time.strftime('%Y-%m-%d',time.localtime(time.time())) + '_' + str(i)        
        i += 1
        path = imgFolder + suffix
        isExists = os.path.exists(path)
        if not isExists:
            os.makedirs(path)
            print(path + '创建成功')
            break
    list = []
    urlFileName = config.urlFileName
    producer = Producer(urlFileName,list)
    driverSize = int(config.driverSize)
    while driverSize > 0:
        driverSize -= 1
        list.append(Consumer('驱动' + str(driverSize), getDriver(),queue.Queue(), path, config))
    producer.start()
    producer.join()    
    for obj in list:
        obj.join()
if __name__ == '__main__':
    print('----------------程序开始----------------')
    start = time.time()
    main()
    end = time.time()
    print('----------------程序花费时间:' + str(end-start)  + '----------------')
    print('----------------程序结束----------------')
    print('----------------按任意键关闭窗口----------------')
    input()